/*
 * @Description: 序列化操作
 * @Version: 1.0
 * @Autor: JiaJun Wu
 * @Date: 2021-09-17 14:03:11
 * @LastEditors: JiaJun Wu
 * @LastEditTime: 2021-09-24 17:56:22
 */
use super::date_time::*;
use crate::utils::{i642_ip, ip2_i64};
use bson::oid::ObjectId;
use core::fmt;
use rocket::serde::{de, Deserializer, Serializer};

/// 序列化 id
#[allow(dead_code)]
pub fn serialize_object_id<S>(oid: &Option<ObjectId>, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match oid.as_ref().map(|x| x.to_hex()) {
        Some(v) => s.serialize_str(&v),
        None => s.serialize_none(),
    }
}

/// 反序列化id
#[allow(dead_code)]
pub fn deserialize_object_id<'de, D>(deserializer: D) -> Result<Option<ObjectId>, D::Error>
where
    D: Deserializer<'de>,
{
    struct Visitor;

    impl<'de> de::Visitor<'de> for Visitor {
        type Value = Option<ObjectId>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("an object id hash value")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v.is_empty() {
                return Ok(None);
            }
            Ok(ObjectId::with_string(v).ok())
        }
    }

    deserializer.deserialize_any(Visitor)
}

/// 序列化 String
#[allow(dead_code)]
pub fn serialize_string<S>(string: &Option<String>, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match string.as_ref().map(|x| x) {
        Some(v) => s.serialize_str(&v),
        None => s.serialize_none(),
    }
}
/// 反序列化 String
#[allow(dead_code)]
pub fn deserialize_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
    D: Deserializer<'de>,
{
    struct Visitor;

    impl<'de> de::Visitor<'de> for Visitor {
        type Value = Option<String>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("an object id hash string")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v.is_empty() {
                return Ok(None);
            }
            Ok(Some(v.to_string()))
        }
    }

    deserializer.deserialize_any(Visitor)
}

/// 序列化 i32
#[allow(dead_code)]
pub fn serialize_i32<S>(n: &Option<i32>, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match n.as_ref().map(|x| x) {
        Some(v) => s.serialize_i32(*v),
        None => s.serialize_none(),
    }
}
/// 反序列化 i32
#[allow(dead_code)]
pub fn deserialize_i32<'de, D>(deserializer: D) -> Result<Option<i32>, D::Error>
where
    D: Deserializer<'de>,
{
    struct Visitor;

    impl<'de> de::Visitor<'de> for Visitor {
        type Value = Option<i32>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("an object id hash i32")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v.is_empty() {
                return Ok(None);
            }
            Ok(Some(v.parse().unwrap_or(0)))
        }
    }

    deserializer.deserialize_any(Visitor)
}

/// 序列化日期 date
#[allow(dead_code)]
pub fn serialize_date<S>(date: &Option<i64>, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match date.map(|x| x) {
        Some(v) => s.serialize_str(&format!("{}", date_to_str(v))),
        None => s.serialize_none(),
    }
}

/// 反序列化日期 date
#[allow(dead_code)]
pub fn deserialize_date<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
where
    D: Deserializer<'de>,
{
    struct Visitor;

    impl<'de> de::Visitor<'de> for Visitor {
        type Value = Option<i64>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("日期转换失败")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(Some(date_str_2_i64(v)))
        }
    }

    deserializer.deserialize_any(Visitor)
}

/// 序列化Ip
#[allow(dead_code)]
pub fn serialize_ip<S>(ip: &Option<i64>, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match ip.as_ref().map(|x| x) {
        Some(v) => s.serialize_str(&format!("{}", &i642_ip(v))),
        None => s.serialize_none(),
    }
}

/// 反序列化ip
#[allow(dead_code)]
pub fn deserialize_ip<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
where
    D: Deserializer<'de>,
{
    struct Visitor;

    impl<'de> de::Visitor<'de> for Visitor {
        type Value = Option<i64>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("ip转换失败")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            if v.is_empty() {
                return Ok(None);
            }
            return Ok(Some(ip2_i64(v)));
        }
    }

    deserializer.deserialize_any(Visitor)
}
